-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Packed, typed object ids #229
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be nice to implement a few more Into
trait for `ObjectId:
- ArrayString
- String
- T
... andFrom
- [u32;3]
- ArrayString
- String
It'd make using ObjectId
much more convivial.
You might argue this is best left for another PR though.
/// ``` | ||
pub fn from_packed_js_val(packed_val: Reference) -> Result<Self, ConversionError> { | ||
let mut packed = [0u32; 3]; | ||
// TODO: make this more efficient, once we get mutable UnsafeTypedArrays. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mind switching this to a tracking issue? We might never see it again otherwise :P
(Feel free to leave the todo)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened #230.
src/objects.rs
Outdated
js_unwrap!(@{self.as_ref()}.id) | ||
/// Retrieves this object's id as an untyped, packed value. | ||
/// | ||
/// This has no major differences from [`HasId::id`] besides the return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
besides -> beside?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right - I think I almost never use besides
correctly...
Edit: looking stuff online seems to come up with conflicting information. I think I'll just switch to "except for" for clarity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meh, I'm not married to either, hence the question mark. Feel free to leave it like that.
With the other conversions - I'm happy to add I think |
You're right. Maybe an expensive |
Sounds good! I forgot you mentioned There was a similar |
I think a combination of There is also potential room for a threadlocal LRU which is per-tick reset caching objects by ID to references, reducing calls out to JS. |
Why not Otherwise, I'm certainly not against |
I'd argue against it mostly because it doesn't feel like an The other reason would be ease of use, since we would be implementing |
Alright, you make a good case, I agree with you! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the additions!
Resolves #51, resolve #192.
This is based on an initial implementation @Dessix made, but I removed some of the optimizations in favor of simpler code, and created a separate untyped version.
This implements the binary/packed 12-byte storage described in #192 for object ids, and implements fairly efficient transfers of these 12-byte values between rust and JS. All of the translations, formatting and parsing are unit tested for reliability (since there's no reliance on a screeps server for that)
This also implements a typed version of the id,
ObjectId<T>
. The type is purely for type inference and optional safety - it has ainto_type()
method to change the inner type arbitrarily, and can be freely transformed into and from untyped object ids. My thought was that allowing this kind of "free" transmutation would mean we can have serialization and deserialization which just works, and doesn't have to do anything like calling into JS and checkingGame.getObjectById()
to ensure it's the right type. It means we need that one technically uneccessary check if you get an ID from a thing and then immediately useget_object_typed
during the same tick, but I think the advantages are worth it.